package com.sttk.secreconeclick.bookkeep.adapter;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lzb.basic.ProviderMultiAdapter;
import com.lzb.basic.utils.SPUtil;
import com.sttk.secreconeclick.bookkeep.Content;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.bean.BillListBean;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;

public class BillAdapter extends ProviderMultiAdapter<BillListBean> {


    public BillAdapter() {
        addItemProvider(new PhoneAlbumItemProvider());
    }


    private class PhoneAlbumItemProvider extends BaseItemProvider<BillListBean> {
        @Override
        public int getItemViewType() {
            return ProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_bill;
        }

        @Override
        public void convert(@NotNull BaseViewHolder baseViewHolder, BillListBean bean) {
            int cycle = SPUtil.getInt(context, Content.cycle, 0);
            if (cycle == 2) {
                baseViewHolder.setText(R.id.tvBillItemDate, TimeUtils.millis2String(bean.getDate(), new SimpleDateFormat("yyyy年MM月dd日，EEE")));
            } else {
                baseViewHolder.setText(R.id.tvBillItemDate, TimeUtils.millis2String(bean.getDate(), new SimpleDateFormat("MM月dd日，EEE")));
            }
            // 格式化金额保留两位小数
            String formattedIncome = String.format("%.2f", bean.getIncome());
            String formattedExpenditure = String.format("%.2f", bean.getExpenditure());
            baseViewHolder.setText(R.id.tvBillItemMoney, context.getString(R.string.bill_list_money,formattedIncome, formattedExpenditure));

            RecyclerView rvBillChild = baseViewHolder.getView(R.id.rvBillChild);
            rvBillChild.setLayoutManager(new LinearLayoutManager(getContext()));
            BillChildAdapter billChildAdapter = new BillChildAdapter();
            rvBillChild.setAdapter(billChildAdapter);
            billChildAdapter.setList(bean.getBillBeanList());
            billChildAdapter.setOnItemClickListener(getOnItemClickListener());
            billChildAdapter.setOnItemLongClickListener(getOnItemLongClickListener());
        }
    }
}
